<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * Error Controller
 *
 * @package		SimplyPost
 * @subpackage	Error
 * @category	Controller
 * @author		Pascal Kriete
 */

class Error extends BaseController {

	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function Error()
	{
		parent::BaseController();
	}

	// --------------------------------------------------------------------

	/**
	 * Shows error pages depending on the type of error
	 *
	 * @access	private
	 */
	function _remap()
	{
		$err = $_SESSION['error'];
		unset($_SESSION['error']);
		
		switch($err['template'])
		{
			case 'error_404':
				$this->_error_404($err['heading'], $err['message']);
				break;
			case 'error_php':
				$this->_error_php($err['severity'], $err['message'], $err['filepath'], $err['line']);
				break;
			case 'error_db':
				$this->_error_db($err['heading'], $err['message']);
				break;
			case 'error_general':
			default:
				$this->_error_general($err['heading'], $err['message']);
		}
	}

	// --------------------------------------------------------------------

	/**
	 * Display General Errors
	 *
	 * @access	private
	 * @param	string	heading
	 * @param	string	error message
	 * @return	description
	 */
	function _error_general($heading, $message)
	{
		set_title('Error');
		$this->breadcrumb->custom(array('Error'));
		
		$data = array(
			'heading' => $heading,
			'message' => $message
		);
		
		$this->parser->parse('error/general', $data);
	}

	// --------------------------------------------------------------------
	
	/**
	 * Display 404 Errors
	 *
	 * @access	private
	 * @param	string	heading
	 * @param	string	error message
	 */
	function _error_404($heading, $message)
	{
		header("HTTP/1.1 404 Not Found");

		set_title('404 Not Found');
		$this->breadcrumb->custom(array('Error', '404'));

		$data = array(
			'heading' => $heading,
			'message' => $message
		);

		$this->parser->parse('error/404', $data);
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Display PHP Errors
	 *
	 * @access	private
	 * @param	string	severity
	 * @param	string	message
	 * @param	string	filepath
	 * @param	integer	line
	 */
	function _error_php($severity, $message, $filepath, $line)
	{		
		set_title('PHP Error');
		$this->breadcrumb->custom(array('Error', 'PHP'));

		$data = array(
			'severity'	=> $severity,
			'message'	=> $message,
			'filepath'	=> $filepath,
			'line'		=> $line,
			'template'	=> 'error_php'
		);

		$this->parser->parse('error/php', $data);
	}

	// --------------------------------------------------------------------

	/**
	 * Display Database Errors
	 *
	 * @access	private
	 * @param	string	heading
	 * @param	string	error message
	 * @return	description
	 */
	function _error_db($heading, $message)
	{
		set_title('Database Error');
		$this->breadcrumb->custom(array('Error', 'DB'));

		$data = array(
			'heading' => $heading,
			'message' => $message
		);
		
		$this->parser->parse('error/db', $data);
	}
	
}